<%@ page language="java" import="java.util.*,java.text.DecimalFormat" pageEncoding="UTF-8"%>
<%@ page import="dragon.core.io.LogicFile"%>
<jsp:directive.page import="dragon.core.util.GlobalParameter;" />
<%@ include file="../../taglib.jsp"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" />
<title>在线文件管理</title>
<%@ include file="/webmodules/console/css.jsp"%>
<%@ include file="/webmodules/console/js.jsp"%>
<script language="javascript">
	<%-- 当前目录路径 --%>
	var basePath = '${param.path}';
	var currentPath = '${param.path}';
	<%-- 复制或剪切相关 --%>
	var sourcefiles = new Array();
	var optype;
	$(function() {
		$('#mydatagrid').datagrid({
			singleSelect : false,
			remoteSort : false,
			rownumbers : true,
			columns : [ [ {
				checkbox : true
			}, {
				field : 'NAME',
				title : '名称',
				width : 200,
				sortable : true,
				formatter : function(val, row) {
					if (row.ISFOLDER == true) {
						return "<a href='javascript:void(0);' onclick='goin(\"" + val + "\");'>" + val + "</a>";
					} else {
						return "<a href='"+(currentPath==""?"":currentPath+"\\")+val+"' target=\"_blank\">" + val + "</a>";
					}
				}
			}, {
				field : 'LASTMODIFYTIME',
				title : '最后修改时间',
				width : 150,
				sortable : true
			}, {
				field : 'CREATETIME',
				title : '创建时间',
				width : 150,
				sortable : true
			}, {
				field : 'SIZE',
				title : '大小',
				width : 100,
				sortable : true,
				formatter : function(val, row) {
					if (val > 1024 && val < 1024 * 1024) {
						return Math.round(val / 1024 * 100) / 100 + " KB";
					} else if (val > 1024 * 1024 && val < 1024 * 1024 * 1024) {
						return Math.round(val / (1024 * 1024) * 100) / 100 + " MB";
					} else {
						return val + " B";
					}
				}
			}, {
				field : 'ISFOLDER',
				hidden : true
			} ] ],
			onLoadError : function() {
				top.showErrorMsg("出错了", "加载数据遇到错误，请稍后重试！");
			},
			onLoadSuccess : function(data) {
				if (data.errors != undefined && data.errors != null) {
					top.showErrorMsg("出错了", "加载数据遇到错误！" + data.errors);
				}
				if (data.sessiontimeout != undefined && data.sessiontimeout == true) {
					top.showMsg("出错了", "会话已过期，请按 &lt;F5&gt; 刷新页面并重新登录到系统！", true);
				}
				setMenu();
			},
			onClickRow : function(rowIndex, rowData) {
				setMenu();
			},
			onCheck : function(rowIndex, rowData) {
				setMenu();
			},
			onUncheck : function(rowIndex, rowData) {
				setMenu();
			},
			onCheckAll : function(rows) {
				setMenu();
			},
			onUncheckAll : function(rows) {
				setMenu();
			}
		});
		load(currentPath);
	});
	<%-- 设置菜单 --%>
	function setMenu(){
		if (currentPath == basePath) {
			$('#menu_toRoot').linkbutton('disable');
			$('#menu_toUp').linkbutton('disable');
		} else {
			$('#menu_toRoot').linkbutton('enable');
			$('#menu_toUp').linkbutton('enable');
		}
		
		var selections = $('#mydatagrid').datagrid('getSelections');
		if(selections.length>0){
			$('#menu_del').linkbutton('enable');
			$('#menu_copy').linkbutton('enable');
			$('#menu_cut').linkbutton('enable');
		}else{
			$('#menu_del').linkbutton('disable');
			$('#menu_copy').linkbutton('disable');
			$('#menu_cut').linkbutton('disable');
		}
		
		if(selections.length != 1){
			$('#menu_modifyFile').linkbutton('disable');
			$('#menu_rename').linkbutton('disable');
		}else{
			if(selections[0].ISFOLDER==true){
				$('#menu_modifyFile').linkbutton('disable');
			}else{
				if(canEdit(selections[0].NAME)){
					$('#menu_modifyFile').linkbutton('enable');
				}else{
					$('#menu_modifyFile').linkbutton('disable');
				}
			}
			$('#menu_rename').linkbutton('enable');
		}
	}
	<%-- 加载指定目录下文件(夹) --%>
	function load(path) {
		if (path != undefined) {
			currentPath = path;
		}
		$('#path').html(currentPath);
		if ($('#mydatagrid').datagrid('options').url == null) {
			$('#mydatagrid').datagrid('options').url = 'dragon/modules/file/action/FileHandler/list.ejw';
		}
		$('#mydatagrid').datagrid('load', {
			path : currentPath
		});
	}
	<%-- 进入某个目录 --%>
	function goin(subpath) {
		if(currentPath==''){
			load(subpath);
		}else{
			load(currentPath + '\\' + subpath);
		}
	}
	<%-- 返回到根目录 --%>
	function toRoot() {
		load(basePath);
	}
	<%-- 返回到上级目录 --%>
	function toUp() {
		var index = -1;
		if ((index = currentPath.lastIndexOf("\\")) != -1) {
			load(currentPath.substring(0, index));
		}else if(currentPath != basePath){
			load(basePath);
		}
	}
	<%-- 创建目录 --%>
	function createFolder() {
		top.showWindow('createFolder', '创建文件夹', "webmodules/file/createFolder.jsp?path=" + currentPath, 350, 120, false);
	}
	<%-- 创建文件 --%>
	function createFile() {
		top.showWindow('editFile', '创建文件', "webmodules/file/editFile.jsp?action=create&path=" + currentPath, 800, 500, true, true);
	}
	<%-- 修改文件 --%>
	function modifyFile() {
		var filename = $('#mydatagrid').datagrid('getSelected').NAME;
		top.showWindow('editFile', '修改文件', "dragon/modules/file/action/FileHandler/preModFile.ejw?action=modify&path=" + currentPath+"&filename="+filename, 800, 500, true, true);
	}
	<%-- 重命名 --%>
	function rename(name) {
		var filename = $('#mydatagrid').datagrid('getSelected').NAME;
		top.showWindow('rename', '重命名', "webmodules/file/rename.jsp?path=" + currentPath + "&filename=" + filename, 350, 180, false);
	}
	<%-- 删除--%>
	function del() {
		top.showConfirm("删除确认", "确认删除吗?", function() {
			var selections = $('#mydatagrid').datagrid('getSelections');
			var filename = new Array(selections.length);
			for(var i=selections.length-1;i>=0;i--){
				filename[i]=selections[i].NAME;
			}
			$.post("dragon/modules/file/action/FileHandler/del.ejw", {
				path:currentPath,
				filename : filename.join(",")
			}, function(data) {
				try{
					data = eval('(' + data + ')'); 
				}catch(e){
					top.showErrorMsg("出错了", data);
					return;
				}
				if(data.MESSAGE_ERROR != undefined && data.MESSAGE_ERROR != ''){
					top.showMsg("错误", data.MESSAGE_ERROR, true,'error');
				}else if(data.MESSAGE_WARNING != undefined && data.MESSAGE_WARNING != ''){
					top.showMsg("警告", data.MESSAGE_WARNING, true,'warning');
				}else{
					if(data.MESSAGE_INFO != undefined && data.MESSAGE_INFO != ''){
						top.showMsg("", data.MESSAGE_INFO, false);
					}else{
						top.showMsg("", "删除成功！", false);
					}
					for ( var i = selections.length-1; i >=0; i--) {
						var index = $('#mydatagrid').datagrid('getRowIndex', selections[i]);
						$('#mydatagrid').datagrid('deleteRow', index);
					}
					selections = null;
				}
			});
		}, null);
	}
	<%-- 删除
	p_optype 操作类型：复制copy，剪切cut
	--%>
	function move(p_optype){
		while (sourcefiles.pop() != null) {}
		var selections = $('#mydatagrid').datagrid('getSelections');
		for(var i=selections.length-1;i>=0;i--){
			sourcefiles.push(currentPath+"\\"+selections[i].NAME);
		}
		optype = p_optype;
		$('#menu_paste').linkbutton('enable');
	}
	<%-- 粘贴--%>
	function paste(){
		top.showConfirm("操作确认", "确认"+(optype=='copy'?'复制':'剪切')+"吗?", function() {
			$.post("dragon/modules/file/action/FileHandler/paste.ejw", {
				target:currentPath,
				sourcefiles : sourcefiles.join(","),
				optype : optype
			}, function(data) {
				try{
					data = eval('(' + data + ')'); 
				}catch(e){
					top.showErrorMsg("出错了", data);
					return;
				}
				if(data.MESSAGE_ERROR != undefined && data.MESSAGE_ERROR != ''){
					top.showMsg("错误", data.MESSAGE_ERROR, true,'error');
				}else if(data.MESSAGE_WARNING != undefined && data.MESSAGE_WARNING != ''){
					top.showMsg("警告", data.MESSAGE_WARNING, true,'warning');
				}else{
					if(data.MESSAGE_INFO != undefined && data.MESSAGE_INFO != ''){
						top.showMsg("", data.MESSAGE_INFO, false);
					}else{
						top.showMsg("", (optype=='copy'?'复制':'剪切')+"成功！", false);
						$('#menu_paste').linkbutton('disable');
					}
					load();
				}
			});
		}, null);
	}
	<%-- 上传文件--%>
	function upload() {
		top.showWindow('upload', '上传文件', "webmodules/file/upload.jsp?path=" + currentPath, 350, 300, false);
	}
	<%-- 打包 --%>
	function zip() {
		top.showConfirm("操作确认", "确认打包当前目录吗?", function() {
			$.post("dragon/modules/file/action/FileHandler/zip.ejw", {
				path:currentPath
			}, function(data) {
				try{
					data = eval('(' + data + ')'); 
				}catch(e){
					top.showErrorMsg("出错了", data);
					return;
				}
				if(data.MESSAGE_ERROR != undefined && data.MESSAGE_ERROR != ''){
					top.showMsg("错误", data.MESSAGE_ERROR, true,'error');
				}else if(data.MESSAGE_WARNING != undefined && data.MESSAGE_WARNING != ''){
					top.showMsg("警告", data.MESSAGE_WARNING, true,'warning');
				}else{
					if(data.MESSAGE_INFO != undefined && data.MESSAGE_INFO != ''){
						top.showMsg("", data.MESSAGE_INFO, false);
					}else{
						top.showMsg("", "打包成功！", false);
					}
					load();
				}
			});
		}, null);
	}
</script>
</head>
<body>
	<div id="mydatagrid" fit="true" toolbar="#tb"></div>
	<div id="tb" style="height: 50px">
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-reload" plain="true" style="float: left;" onclick="load();">刷新</a>
		<div class="datagrid-btn-separator"></div>
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-undo" plain="true" style="float: left;" onclick="toRoot();" id="menu_toRoot">根</a>
		<div class="datagrid-btn-separator"></div>
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-back" plain="true" style="float: left;" onclick="toUp();" id="menu_toUp">向上</a>
		<div class="datagrid-btn-separator"></div>
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" disabled="true" style="float: left;" onclick="del();" id="menu_del">删除</a>
		<div class="datagrid-btn-separator"></div>
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="ext-icon-folder_add" plain="true" style="float: left;" onclick="createFolder();">创建目录</a>
		<div class="datagrid-btn-separator"></div>
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="ext-icon-file_add" plain="true" style="float: left;" onclick="createFile();">创建文件</a>
		<div class="datagrid-btn-separator"></div>
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="ext-icon-upload" plain="true" style="float: left;" onclick="upload();">上传文件</a>
		<div class="datagrid-btn-separator"></div>
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="ext-icon-package" plain="true" style="float: left;" onclick="zip();">打包</a>
		<div class="datagrid-btn-separator"></div>
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="ext-icon-edit" plain="true" disabled="true" style="float: left;" onclick="modifyFile();" id="menu_modifyFile">修改文件</a>
		<div class="datagrid-btn-separator"></div>
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-edit" plain="true" disabled="true" style="float: left;" onclick="rename();" id="menu_rename">重命名</a>
		<div class="datagrid-btn-separator"></div>
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="ext-icon-copy" plain="true" disabled="true" style="float: left;" onclick="move('copy');" id="menu_copy">复制</a>
		<div class="datagrid-btn-separator"></div>
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-cut" plain="true" disabled="true" style="float: left;" onclick="move('cut');" id="menu_cut">剪切</a>
		<div class="datagrid-btn-separator"></div>
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="ext-icon-paste" plain="true" disabled="true" style="float: left;" onclick="paste();" id="menu_paste">粘贴</a>
		<div style="clear: left;float: left;line-height: 25px;margin: 0 3 0 3">
			<label>当前路径:</label> <label id="path" style="font-weight: bold; font-size: medium;"></label>
		</div>
	</div>
</body>
</html>
